Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.sendMobileCode = sendMobileCode, exports.resolverWxMobile = resolverWxMobile;

var _api = _interopRequireDefault(require("../../utils/api")), _user = _interopRequireDefault(require("../../utils/user")), _apiCode = _interopRequireDefault(require("../../utils/api-code"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _objectSpread(t) {
    for (var e = 1; e < arguments.length; e++) {
        var r = null != arguments[e] ? arguments[e] : {}, o = Object.keys(r);
        "function" == typeof Object.getOwnPropertySymbols && (o = o.concat(Object.getOwnPropertySymbols(r).filter(function(e) {
            return Object.getOwnPropertyDescriptor(r, e).enumerable;
        }))), o.forEach(function(e) {
            _defineProperty(t, e, r[e]);
        });
    }
    return t;
}

function _defineProperty(e, t, r) {
    return t in e ? Object.defineProperty(e, t, {
        value: r,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[t] = r, e;
}

function sendMobileCode(e) {
    return new Promise(function(t, r) {
        _api.default.users.sendcode({
            source: "weApp",
            mobile: e
        }).then(function(e) {
            e.code === _apiCode.default.SUCCESS && e.data.issend ? t() : r();
        });
    });
}

var getMobileRetryCount = 0, GETMOBILE_RETRY_MAX = 3, GETMOBILE_RETRY_INVERTER = 2e3;

function retryGetMobile(o, i) {
    return new Promise(function(e, t) {
        if (++getMobileRetryCount < GETMOBILE_RETRY_MAX) var r = setTimeout(function() {
            clearTimeout(r), e(_user.default.getWxLoginCode().then(function(e) {
                return getUserMobile(e, o, i);
            }));
        }, GETMOBILE_RETRY_INVERTER); else t();
    });
}

var getUserMobile = function(e, t, r) {
    return _api.default.weapp.getUserMobile({
        data: _objectSpread({}, e, {
            iv: t.iv,
            encryptedData: t.encryptedData,
            app: r
        })
    }).then(function(e) {
        return e.code === _apiCode.default.SUCCESS ? Promise.resolve(e) : e.code === _apiCode.default.SESSIONKEY_EXPIRY ? retryGetMobile(t, r) : Promise.reject();
    });
};

function resolverWxMobile(o, i, n) {
    return getMobileRetryCount = 0, new Promise(function(t, r) {
        var e = getApp().reportAnalytics;
        o.encryptedData && o.iv ? (e("auth_mobile_confirm"), wx.showLoading({
            title: "获取中...",
            mask: !0
        }), (n ? Promise.resolve({
            code: n
        }) : _user.default.getLoginCodeOrSessionkey()).then(function(e) {
            getUserMobile(e, o, i).then(t, r).then(wx.hideLoading, wx.hideLoading);
        })) : (r(), e("auth_mobile_cancle"));
    });
}